<!DOCTYPE html>
<meta charset="UTF-8"> 
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-125261808-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-125261808-1');
</script>
<link href='https://fonts.googleapis.com/css?family=Roboto Condensed' rel='stylesheet'>
<style>

body {
  margin-top: 5vw;
  margin-left: 10vw;
  margin-right: 10vw;
   padding: 0;
  font-size: 100%;
  font-family: 'Roboto Condensed', 'Tahoma', 'Arial', sans-serif;
}


   ul.tree a {
    color: back;
    text-decoration: none;
    display: block;
    width: 100%;
   }
   ul.tree a:hover {
    background: lightgray;
    text-decoration: none;
    display: inline-block;
    width: 100%;
   }
img.logo {max-width: 300px;}

   li {
     list-style-type: none;
     background:  repeat-y;
     padding: 0;
   }


   ul.tree, ul.tree ul {
     list-style-type: none;
     background:  repeat-y;
     margin: 0;
     padding: 0;
     margin: 0.1vw;
   }

   ul.tree ul {
     list-style-type: none;
     margin-left: 0.1vw;
   }

ul.tree li {
     margin: 0.1vw;
     padding: 0.1vw 1vw;
     line-height: 100%;
     color: #369;
   }


a {
    color: black;
}


   ul.tree li.last {
     background: #fff url(images/lastnode.png) no-repeat;
   }

   ul.topnav {
    list-style-type: none;
    padding: 0;
    margin: 0;
    overflow: hidden;
    background-color: #333;
    font-size: 1.5vw;
    text-align: center;
}

ul.topnav li {float: left;
     list-style-type: none;
}

ul.topnav li a {
    display: block;
    color: white;
    text-align: center;
    padding: 0.2vw 0.0cm;
    width: 8.5vw;
    text-decoration: none;
    font-size: 1.2vw;
}
ul.topnav li a:hover:not(.active) {background-color: #111;}
ul.topnav li a.active {background-color: #4CAF50;}
ul.topnav li.right {float: right;}


pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }

@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }

span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }

.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }

table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }

img.logo {max-width: 25vw;}

.caret {
  cursor: pointer;
  -webkit-user-select: none; /* Safari 3.1+ */
  -moz-user-select: none; /* Firefox 2+ */
  -ms-user-select: none; /* IE 10+ */
  user-select: none;
}

.caret::before {
  content: "\25B6";
  color: black;
  display: inline-block;
  margin-right: 6px;
}

.caret-down::before {
  -ms-transform: rotate(90deg); /* IE 9 */
  -webkit-transform: rotate(90deg); /* Safari */'
  transform: rotate(90deg);  
}

.nested {
  display: none;
}

.active {
  display: block;
}

 ul.tree a:hover {
    background: lightgray;
    text-decoration: none;
    display: inline-block;
    width: 100%;
   }


   ul.simple li {
     list-style-type: square;
     background:  repeat-y;
     padding: 0;
   }


</style>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML"></script>
</head>
<body> 
<img class="logo" src="ValoMC_logo.png">
<ul class="topnav">
<font size="4">
  <li><a href="index.html">Home</a></li>
  <li><a href="download.html">Download</a></li>
  <li><a href="features.html">Features</a></li>
  <li><a href="installation.html">Installation</a></li>
  <li><a class="active" href="documentation.html">Documentation</a></li>
  <li class="right"><a href="about.html">About</a></li>
</font>
</ul>

<div style="padding:0 0vw; width: auto;">
  <h2></h2>
  <p></p>
  <p></p>
<font size="4">
<div class="bottomhalf" style="width: 100%;">
    <div style="width: 20%; float: left;">
<br> 
<ul style="padding: 0 0px" class="tree">
<li>
<span class="caret"> Documentation</span>
<ul class="nested">
<li>
<a href="gettingstarted.html"> Getting started</a>
</li>
<li>
<span class="caret"> 2D Code documentation</span>
<ul class="nested">
<li>
<a href="structures.html"> List of structures</a>
</li>
<li>
<a href="findingelements.html"> Finding elements</a>
</li>
<li>
<a href="findingboundaries.html"> Finding boundaries</a>
</li></ul>
<li>
<span class="caret"> 2D Examples</span>
<ul class="nested">
<li>
<a href="simpletest.html"> Simple example</a>
</li>
<li>
<a href="inhomogeneous.html"> Creating an inhomogeneous medium</a>
</li>
<li>
<a href="directingls.html"> Directing light sources</a>
</li>
<li>
<a href="boundarytest.html"> Setting boundary conditions and visualising the boundary solution</a>
</li>
<li>
<a href="frequency.html"> Frequency domain calculation</a>
</li>
<li>
<a href="netgentest.html"> Working with NetGen</a>
</li>
<li>
<a href="generatingc.html"> Generating input for the external executable</a>
</li>
<li>
<a href="pixeltest.html"> Working with pixel format data</a>
</li>
<li>
<a href="toasttest.html"> Working with Toast++</a>
</li>
<li>
<a href="kwavetest.html"> Simulating the photoacoustic effect using K-Wave</a>
</li></ul>
<li>
<span class="caret"> 3D Code documentation</span>
<ul class="nested">
<li>
<a href="structures3d.html"> List of structures</a>
</li>
<li>
<a href="findingelements3d.html"> Finding elements</a>
</li>
<li>
<a href="findingboundaries3d.html"> Finding boundaries</a>
</li></ul>
<li>
<span class="caret"> 3D Examples</span>
<ul class="nested">
<li>
<a href="voxeltest.html"> Working with voxel format data</a>
</li>
<li>
<a href="netgentest3d.html"> Working with NetGen</a>
</li>
<li>
<a href="threedmodel.html"> Digimouse example</a>
</li></ul>
<li>
<a href="functionreference.html"> Alphabetical function listing</a>
</li></ul>
<li>
<a href="literature.html"> Literature</a>
</li>
</ul>
    </div>
    <div style="margin-left: 20%; padding: 1vw;" class="maintext"> 
<div class="content"><h1>Directing light sources: directingls.m</h1><!--introduction--><p>This example demonstrates how to give custom directions to the light sources and how to use different built-in directivity patterns.</p><!--/introduction--><h2>Contents</h2><div><ul><li><a href="#1">Set up the mesh and the medium</a></li><li><a href="#2">Set up the boundary and create light sources</a></li><li><a href="#4">Create and direct lightsources using different directivity patterns</a></li><li><a href="#5">Run the Monte Carlo simulation</a></li><li><a href="#6">Plot the solution</a></li></ul></div><h2 id="1">Set up the mesh and the medium</h2><pre class="codeinput"><span class="comment">% Create a rectangular mesh</span>
xsize =  10;    <span class="comment">% width of the region [mm]</span>
ysize =  10;    <span class="comment">% width of the region [mm]</span>
dh = 0.1;       <span class="comment">% discretisation size [mm]</span>
vmcmesh = createRectangularMesh(xsize, ysize, dh);

<span class="comment">% Set constant background coefficients</span>
vmcmedium.absorption_coefficient = 0.03;     <span class="comment">% absorption coefficient [1/mm]</span>
vmcmedium.scattering_coefficient = 0.3;      <span class="comment">% scattering coefficient [1/mm]</span>
vmcmedium.scattering_anisotropy = 0.9;       <span class="comment">% scattering anisotropy parameter [unitless]</span>
vmcmedium.refractive_index = 1.3;            <span class="comment">% refractive index [unitless]</span>

<span class="comment">% Turn the fields in vmcmedium into arrays so that each coefficient can be</span>
<span class="comment">% set individually for each element</span>
vmcmedium = createMedium(vmcmesh, vmcmedium);

rectangle_width = 1.9;
rectangle_height = 1.9;
rectangle_position=[-xsize/4 ysize/4];

<span class="comment">% Find elements that are inside of a rectangle.</span>
<span class="comment">% The rectangle is shown in the figure below.</span>
elements_of_the_rectangle = findElements(vmcmesh, <span class="string">'rectangle'</span>, <span class="keyword">...</span>
                                         rectangle_position, <span class="keyword">...</span>
                                         rectangle_width, <span class="keyword">...</span>
                                         rectangle_height);

<span class="comment">% Set the optical coefficients inside the rectangle</span>
vmcmedium.absorption_coefficient(elements_of_the_rectangle) = 0.5;
vmcmedium.scattering_anisotropy(elements_of_the_rectangle) = 0.0;
</pre><h2 id="2">Set up the boundary and create light sources</h2><p>createBoundary returns a structure which can be used to set the properties of each boundary element individually</p><pre class="codeinput">vmcboundary = createBoundary(vmcmesh);
</pre><p>Set up 4 lightsources using 4 lines. The lines are shown in the figure below.</p><p><img alt="" hspace="5" src="directingls_lines.png" vspace="5"/> </p><pre class="codeinput">line1_start = [-3/4*xsize -ysize*1/4];
line1_end = rectangle_position;

rectangle_diameter = sqrt(rectangle_width^2+rectangle_height^2);
line_width=rectangle_diameter;

line2_start = [0 -3/5*ysize];
line2_end = [0 0];

line3_start = [3/5*xsize 0];
line3_end = [0 0];

line4_start = [0 3/5*ysize];
line4_end = [0 0];

lightsource1 = findBoundaries(vmcmesh, <span class="string">'direction'</span>, <span class="keyword">...</span>
                              line1_start, <span class="keyword">...</span>
                              line1_end,  <span class="keyword">...</span>
                              line_width);

lightsource2 = findBoundaries(vmcmesh, <span class="string">'direction'</span>, <span class="keyword">...</span>
                              line2_start, <span class="keyword">...</span>
                              line2_end,  <span class="keyword">...</span>
                              line_width);

lightsource3 = findBoundaries(vmcmesh, <span class="string">'direction'</span>, <span class="keyword">...</span>
                              line3_start, <span class="keyword">...</span>
                              line3_end,  <span class="keyword">...</span>
                              line_width);

lightsource4 = findBoundaries(vmcmesh, <span class="string">'direction'</span>, <span class="keyword">...</span>
                              line4_start, <span class="keyword">...</span>
                              line4_end,  <span class="keyword">...</span>
                              line_width);
</pre><h2 id="4">Create and direct lightsources using different directivity patterns</h2><pre class="codeinput"><span class="comment">% 1: Direct light source</span>
<span class="comment">%</span>
<span class="comment">% The 'lightsource' -field in vmcboundary sets the directivity pattern of</span>
<span class="comment">% the lightsource. By 'direct' keyword, all photons are launched in the</span>
<span class="comment">% same direction.</span>

vmcboundary.lightsource(lightsource1) = {<span class="string">'direct'</span>};

<span class="comment">% Create a direction vector for the light using the line that was used to</span>
<span class="comment">% search boundary elements</span>
lightsource_direction = line1_end - line1_start;

<span class="comment">% x-component of the direction</span>
vmcboundary.lightsource_direction(lightsource1,1) = lightsource_direction(1);
<span class="comment">% y-component of the dircetion</span>
vmcboundary.lightsource_direction(lightsource1,2) = lightsource_direction(2);
<span class="comment">% This means that the direction vector is given in the coordinate space of</span>
<span class="comment">% the mesh</span>
vmcboundary.lightsource_direction_type(lightsource1) = {<span class="string">'absolute'</span>};

<span class="comment">% 2: A Gaussian light source</span>
<span class="comment">%</span>
<span class="comment">% Create a light source with a Gaussian directivity profile. The initial</span>
<span class="comment">% angles with respect to a given direction (by default, normal of the</span>
<span class="comment">% boundary element) follow a Gaussian with sigma = 0.1</span>

vmcboundary.lightsource(lightsource2) = {<span class="string">'gaussian'</span>};
vmcboundary.lightsource_gaussian_sigma(lightsource2) = 0.1;

<span class="comment">% Tilt the lightsource by 22.5 degrees. This time, the direction is given in</span>
<span class="comment">% the coordinate system of the boundary element: (0, 1) is the normal</span>
<span class="comment">% direction and (1, 0) is directed along the boundary element</span>
vmcboundary.lightsource_direction(lightsource2,1) = sin(-pi/8);
vmcboundary.lightsource_direction(lightsource2,2) = cos(-pi/8);

<span class="comment">% this direction was given with respect to the surface normal</span>
vmcboundary.lightsource_direction_type(lightsource2) = {<span class="string">'relative'</span>};

<span class="comment">% 3: Cosinic light source</span>
<span class="comment">%</span>
<span class="comment">% The initial angles follow a cosine distribution. Cosinic light sources</span>
<span class="comment">% are useful to create light sources that are not unidirectional without</span>
<span class="comment">% having to set any extra parameters.</span>

vmcboundary.lightsource(lightsource3) = {<span class="string">'cosinic'</span>};

<span class="comment">% 4: Isotropic light source</span>
<span class="comment">%</span>
<span class="comment">% Photons are launched to all inward directions with an equal probability.</span>

vmcboundary.lightsource(lightsource4) = {<span class="string">'isotropic'</span>};
</pre><h2 id="5">Run the Monte Carlo simulation</h2><pre class="codeinput">solution = ValoMC(vmcmesh, vmcmedium, vmcboundary);
</pre><pre class="codeoutput">                 ValoMC-2D
--------------------------------------------
  Version:  v1.0b-118-g853f111
  Revision: 131
  OpenMP enabled                     
  Using 16 threads
--------------------------------------------
Initializing MC2D...
Computing... 
...done

Done
</pre><h2 id="6">Plot the solution</h2><pre class="codeinput">hold <span class="string">on</span>;

patch(<span class="string">'Faces'</span>,vmcmesh.H,<span class="string">'Vertices'</span>,vmcmesh.r,<span class="string">'FaceVertexCData'</span>, solution.element_fluence, <span class="string">'FaceColor'</span>, <span class="string">'flat'</span>,<span class="string">'EdgeColor'</span>,<span class="string">'none'</span>);
xlabel(<span class="string">'[mm]'</span>);
ylabel(<span class="string">'[mm]'</span>);

text(-xsize/2+0.4, 0, <span class="string">'direct lightsource'</span>);
text(0, -ysize/2+0.4, <span class="string">'gaussian tilted by 22.5 deg'</span>, <span class="string">'HorizontalAlignment'</span>, <span class="string">'center'</span>);
text(xsize/2-0.4, 0, <span class="string">'cosinic'</span>, <span class="string">'HorizontalAlignment'</span>, <span class="string">'right'</span>);
text(0, ysize/2-0.4, <span class="string">'isotropic'</span>, <span class="string">'HorizontalAlignment'</span>, <span class="string">'center'</span>);

c = colorbar;
c.Label.String = <span class="string">'Fluence [J/mm^2]'</span>;
hold <span class="string">off</span>
</pre><img alt="" hspace="5" src="directingls_01.png" vspace="5"/> <p class="footer"><br/><a href="http://www.mathworks.com/products/matlab/">Published with MATLAB® R2016b</a><br/></p></div>
    </div>
</div>
</font>
  </div>

<script>


document.body.style.fontSize = (screen.width*0.01).toString() + "px";

document.getElementsByClassName("bottomhalf")[0].style.fontSize = (screen.width*0.01).toString() + "px"

var toggler = document.getElementsByClassName("caret");
var i;

toggler[0].parentElement.querySelector(".nested").classList.toggle("active");
toggler[0].classList.toggle("caret-down");

for (i = 1; i < toggler.length; i++) {
  
    if (localStorage.getItem("vmenutoggler" + i.toString())) {
        myvalue = localStorage.getItem("vmenutoggler" + i.toString());
        if(myvalue) {
          toggler[i].parentElement.querySelector(".nested").classList.toggle("active");
          toggler[i].classList.toggle("caret-down");
         }
    }
}

for (i = 0; i < toggler.length; i++) {
  toggler[i].mynumber = i;
  toggler[i].addEventListener("click", function() {
    this.parentElement.querySelector(".nested").classList.toggle("active");
    if(this.classList.toggle("caret-down")) {
       localStorage.setItem("vmenutoggler" + this.mynumber.toString(), "true");
    } else {
       localStorage.removeItem("vmenutoggler" + this.mynumber.toString());
    }
  });
}

</script>

<br>
<br> </div><footer> <hr> <p>Last updated: Tue Apr  9 11:54:30 EEST 2019 by aleksle</p></footer> </body> </html>

